package com.shopmall.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopmall.system.dao.MenuMapper;
import com.shopmall.system.dao.RoleMenuMapper;
import com.shopmall.system.entity.Menu;
import com.shopmall.system.service.IMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Resource
    RoleMenuMapper roleMenuMapper;
    @Override
    public List<Menu> pageList(Menu role) {
        //查询全部菜单信息
        LambdaQueryWrapper<Menu> menuWrapper = new LambdaQueryWrapper<>();
        menuWrapper.eq(Menu::getPid,0);
        List<Menu> menus = this.baseMapper.selectList(menuWrapper);
        for (Menu menu : menus) {
            LambdaQueryWrapper<Menu> childWrapper = new LambdaQueryWrapper<>();
            childWrapper.eq(Menu::getPid,menu.getId());
            List<Menu> childMenus = this.baseMapper.getMenus(menu.getId());
            menu.setChildren(childMenus);
        }
        return menus;
    }

    @Override
    public int addOrUpdateMenu(Menu menu) {
        int num = 0;
        if(menu.getId()==null){
            num =this.baseMapper.insert(menu);
        }else{
            num =this.baseMapper.updateById(menu);
        }
        return num;
    }

    @Override
    public int deleteMenuById(Integer id) {
        int i = this.baseMapper.deleteById(id);
        if(i>0){
            LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Menu::getPid,id);
            this.baseMapper.delete(wrapper);
        }
        return i;
    }

    @Override
    public List<Map<String, Object>> getRuleMenu() {
        //查询全部菜单信息
        LambdaQueryWrapper<Menu> menuWrapper = new LambdaQueryWrapper<>();
        menuWrapper.eq(Menu::getPid,0);
        List<Menu> menus = this.baseMapper.selectList(menuWrapper);
        for (Menu menu : menus) {
            LambdaQueryWrapper<Menu> childWrapper = new LambdaQueryWrapper<>();
            childWrapper.eq(Menu::getPid,menu.getId());
            List<Menu> childMenus = this.baseMapper.selectList(childWrapper);
            menu.setChildren(childMenus);
        }
        return null;
    }
}
